package com.tuyrk.chapter07;

/**
 * 验证This锁的存在
 *
 * @author tuyrk
 */
public class SynchronizedThis {


	public static void main(String[] args) {
		ThisLock thisLock = new ThisLock();
		new Thread("T1") {
			@Override
			public void run() {
				thisLock.m1();
			}
		}.start();
		new Thread("T2") {
			@Override
			public void run() {
				thisLock.m2();
			}
		}.start();
	}
}

class ThisLock {
	private final Object LOCK = new Object();

	/**
	 * 同步方法
	 */
	public synchronized void m1() {
		try {
			System.out.println(Thread.currentThread().getName());
			Thread.sleep(10_000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 同步代码块
	 */
	public void m2() {
		synchronized (LOCK) {
			try {
				System.out.println(Thread.currentThread().getName());
				Thread.sleep(10_000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
